/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.cgb.portal.bean;

import br.cgb.portal.bo.ActivationCodeBO;
import br.cgb.portal.bo.NewsBO;
import br.cgb.portal.bo.UserBO;
import br.cgb.portal.entity.ActivationCode;
import br.cgb.portal.entity.News;
import br.cgb.portal.entity.User;
import br.cgb.portal.entity.enumeration.Country;
import br.cgb.portal.entity.enumeration.Sex;
import br.cgb.portal.util.EncryptionUtil;
import br.cgb.portal.util.SmtpUtil;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.mail.MessagingException;

/**
 *
 * @author LUCAS
 */
@ManagedBean
@ViewScoped
public class MainBean extends AppBaseBean {

    @EJB
    private ActivationCodeBO activationCodeBO;
    @EJB
    private NewsBO newsBO;
    @EJB
    private UserBO userBO;
    private String email;
    private List<SelectItem> countryList;
    private boolean isPortuguese;
    private boolean isEnglish;

    /**
     * Creates a new instance of MainBean
     */
    public MainBean() {
        isPortuguese = true;
    }

    public void insertAll() {
        User user1 = new User("fulano@gmail.com", EncryptionUtil.encrypt("123456"), Country.BRAZIL, "Fulano", "", Calendar.getInstance(), Sex.MALE, "557999999999", 0L, Calendar.getInstance(), true);
        userBO.create(user1);

        ActivationCode activationCode1 = new ActivationCode(EncryptionUtil.generateActivationCode(), user1);
        activationCodeBO.create(activationCode1);

        News news1 = new News("Teste", Calendar.getInstance(), "", "");
        newsBO.create(news1);
    }

    public void sendEmail() {
        try {
            SmtpUtil.sendEmail(email, EncryptionUtil.generateActivationCode(), getDOMAIN(), getBundle());
        } catch (MessagingException ex) {
            Logger.getLogger(MainBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void toPortuguese() {
        if (isEnglish) {
            setLanguage(new Locale("pt", "BR"));
            isPortuguese = true;
            isEnglish = false;
            loadCountryList();
        }
    }

    public void toEnglish() {
        if (isPortuguese) {
            setLanguage(new Locale("en", "US"));
            isPortuguese = false;
            isEnglish = true;
            loadCountryList();
        }
    }

    private void loadCountryList() {
        countryList = new ArrayList<SelectItem>();
        for (Country c : Country.values()) {
            countryList.add(new SelectItem(c, getBundle().getString(c.toString())));
        }
    }
    /*
     * Getters and setters
     */

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<SelectItem> getCountryList() {
        if (countryList == null) {
            loadCountryList();
        }
        return countryList;
    }

    public void setCountryList(List<SelectItem> countryList) {
        this.countryList = countryList;
    }
}
